import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'userLogin',
    component: () => import('../views/UserLoginView.vue')
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    redirect:'/dashboard/homePage',
    component: () => import('../views/DashBoardView.vue'),
    children:[
      {
        path:'homePage',
        name:'homePage',
        component: () => import('../views/HomePageView.vue')
      },
      {
        path:'heros',
        name:'heros',
        component: () => import('../views/HerosView')
      },
      {
        path:'equips',
        name:'equips',
        component:() => import('../views/EquipsView')
      },
      {
        path:'skills',
        name:'skills',
        component:() => import('../views/SkillsView')
      },
      {
        path:'mings',
        name:'mings',
        component:() => import('../views/MingsView')
      },
      {
        path:'users',
        name:'users',
        component:() => import('../views/UsersView')
      },
      {
        path:'userdetail/:id',
        name:'userdetail',
        component:() => import('../views/UserDetailView')
      },
      {
        path:'adduser',
        name:'adduser',
        component:() => import('../views/AddUserView')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView')
  },
  {
    path: '/userLogin',
    name: 'userLogin',
    component: () => import('../views/UserLoginView')
  },
  {
    path: '/mainlist',
    name: 'mainlist',
    component: () => import('../views/MainListView.vue'),
    children:[
      {
        path:'heroslist',
        name:'heroslist',
        component: () => import('../views/HerosListView')
      },
      {
        path:'equipslist',
        name:'equipslist',
        component: () => import('../views/EquipsListView')
      },
      {
        path:'skillslist',
        name:'skillslist',
        component: () => import('../views/SkillsListView')
      },
    ]
  },
  {
    //:id   接收a标签中href里的  hero.ename参数
    path: '/heroinfo/:id',
    name: 'heroinfo',
    component: () => import('../views/HeroInfoView.vue')
  },
  {
    path: '/hint',
    name: 'hint',
    component: () => import('../views/HintView.vue')
  },
  {
    path: '/*',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
]





const router = new VueRouter({
  routes
})

//路由守卫,拦截验证功能的实现
//引入全局路由守卫 ，注册前置路由守卫
router.beforeEach((to,from,next) => {
  if(to.path === '/userLogin' || to.path === '/' ) {
    //若是去login页面  则放行,去下一个页面
    next(); 
  } else {
    //判断是否登陆过
    //获取会话存储（或者本地存储）中存储的凭证
    // const tokenValue = localStorage.getItem("token_key");
    let value = store.state.isLogin;
    if(value) {
      //若浏览器存储了token  则放行
      next(); 
    }else {
      alert("还没有登录，请先登录...");
      next({path:'/userLogin'});
    }
  }
});

export default router
